//
//  Operation_OtherUse_VC.m
//  DXC
//
//  Created by TOPTEAM on 2017/5/9.
//  Copyright © 2017年 TOPTEAM. All rights reserved.
//

#import "Operation_OtherUse_VC.h"
#import "Other_Operation.h"
@interface Operation_OtherUse_VC ()
- (IBAction)Action:(id)sender;
@property (nonatomic, strong) NSOperationQueue *queue;

@end

@implementation Operation_OtherUse_VC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"NSOperation其他用法";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)Action:(id)sender {
    
    //1.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    //maxConcurrentOperationCount
    /*
     默认是并发队列,如果最大并发数>1,并发
     如果最大并发数==1,串行队列
     系统的默认是最大并发数-1
     
     */
    //    queue.maxConcurrentOperationCount = 1;
    //
    //    //2.封装操作
    //    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
    //        [NSThread sleepForTimeInterval:1.0];
    //        for (NSInteger i =0; i<10000; i++) {
    //            NSLog(@"1--%zd---%@",i,[NSThread currentThread]);
    //        }
    //    }];
    //
    //    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
    //        [NSThread sleepForTimeInterval:1.0];
    //        for (NSInteger i =0; i<10000; i++) {
    //            NSLog(@"2-%zd--%@",i,[NSThread currentThread]);
    //        }
    //
    //    }];
    //
    //    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
    //        [NSThread sleepForTimeInterval:1.0];
    //        NSLog(@"3---%@",[NSThread currentThread]);
    //    }];
    //
    //    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
    //        [NSThread sleepForTimeInterval:1.0];
    //        NSLog(@"4---%@",[NSThread currentThread]);
    //    }];
    //
    //    NSBlockOperation *op5 = [NSBlockOperation blockOperationWithBlock:^{
    //        [NSThread sleepForTimeInterval:1.0];
    //        NSLog(@"5---%@",[NSThread currentThread]);
    //    }];
    //
    //    //3.把操作添加到队列
    //    [queue addOperation:op1];
    //    [queue addOperation:op2];
    //    [queue addOperation:op3];
    //    [queue addOperation:op4];
    //    [queue addOperation:op5];
    
    Other_Operation *op1 = [[Other_Operation alloc]init];
    [queue addOperation:op1];
    
    self.queue = queue;
    
    
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //    suspended
    //    self.queue.suspended = YES;
    //当值为YES的适合暂停,为NO的时候是恢复
    //    if (self.queue.suspended) {
    //        NSLog(@"恢复执行");
    //        self.queue.suspended = NO;
    //    }else
    //    {
    //        NSLog(@"暂停");
    //        self.queue.suspended = YES;
    //    }
    
    //取消,不再执行
    [self.queue cancelAllOperations];
    
}

-(void)maxConcurrentOperationCount
{
    //1.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    //maxConcurrentOperationCount
    /*
     默认是并发队列,如果最大并发数>1,并发
     如果最大并发数==1,串行队列
     系统的默认是最大并发数-1
     
     */
    queue.maxConcurrentOperationCount = 1;
    
    //2.封装操作
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"1---%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"2---%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"3---%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"4---%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *op5 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"5---%@",[NSThread currentThread]);
    }];
    
    //3.把操作添加到队列
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];
    [queue addOperation:op4];
    [queue addOperation:op5];
}




@end























